package org.filetransfer.file_transfer_java_backend.controller;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.ServletRequest;
import org.filetransfer.file_transfer_java_backend.config.RequireAuth;
import org.filetransfer.file_transfer_java_backend.entity.File;
import org.filetransfer.file_transfer_java_backend.entity.ShareItem;
import org.filetransfer.file_transfer_java_backend.service.ShareItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/share")
public class ShareItemController {
    @Autowired
    private ShareItemService shareItemService;
    @GetMapping("/{code}")
    @Operation(summary = "通过分享码获取分享信息")
    public ResponseEntity<ShareItem> getShareItemById(@PathVariable String code) {
        Optional<ShareItem> shareItem = shareItemService.getShareItem(code);
        return shareItem.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
    }
    @GetMapping("/{code}/{md5}/{fileName}")
    @Operation(summary = "下载分享文件")
    public ResponseEntity<InputStreamResource> getShareItemFiles(@PathVariable String code, @PathVariable String md5, @PathVariable String fileName, ServletRequest request) throws FileNotFoundException {
        Optional<File> filesOptional = shareItemService.getShareItemFile(code, md5);
        if (filesOptional.isEmpty()) {
            return ResponseEntity.notFound().build();
        }
        var fileInfo = filesOptional.get();
        var path = fileInfo.getFilePath();
        var fileInstance = new FileInputStream(path);
        var is = new InputStreamResource(fileInstance);
        // 增加下载次数
        shareItemService.incrementDownloadCount(code, md5);

        return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=" + fileName).contentType(org.springframework.http.MediaType.APPLICATION_OCTET_STREAM).contentLength(fileInfo.getFileSize()).body(is);
    }
    public record NewShareItemInfo(List<String> files) {}    @Operation(summary = "通过文件列表创建分享")
    @PostMapping("/create")
    public ShareItemService.ShareItemInfoResult createShareItem(@RequestBody NewShareItemInfo info) {
        return shareItemService.createShareItem(info);
    }    @RequireAuth
    @Operation(summary = "删除分享")
    @PostMapping("/delete/{code}")
    public void deleteShareItem(@PathVariable String code, @RequestBody String editToken) {
        shareItemService.deleteShare(code, editToken);
    }
    
    @RequireAuth
    @Operation(summary = "获取当前用户的分享列表")
    @GetMapping("/my-shares")
    public ResponseEntity<List<ShareItem>> getCurrentUserShares() {
        try {
            List<ShareItem> shares = shareItemService.getCurrentUserShares();
            return ResponseEntity.ok(shares);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
}
